<template>
   <div class="app-container">
      <div class="head-container">
         <a-form :model="queryParams" ref="queryRef" layout="inline">
            <a-form-item prop="noticeTitle">
               <a-input v-model="queryParams.noticeTitle" placeholder="请输入公告标题" clearable style="width: 200px"
                  @keyup.enter="handleQuery" />
            </a-form-item>
            <a-form-item prop="createBy">
               <a-input v-model="queryParams.createBy" placeholder="请输入操作人员" clearable style="width: 200px"
                  @keyup.enter="handleQuery" />
            </a-form-item>
            <a-form-item prop="noticeType">
               <a-select v-model="queryParams.noticeType" placeholder="公告类型" clearable style="width: 200px">
                  <a-option v-for="dict in sys_notice_type" :key="dict.value" :label="dict.label" :value="dict.value" />
               </a-select>
            </a-form-item>
            <a-form-item>
               <a-space>
                  <a-button type="primary" icon="Search" @click="handleQuery">
                     <template #icon>
                        <icon-search />
                     </template>
                     搜索</a-button>
                  <a-button icon="Refresh" @click="resetQuery">
                     <template #icon>
                        <icon-refresh />
                     </template>
                     重置</a-button>
               </a-space>
            </a-form-item>
         </a-form>
      </div>
      <div class="main-container">
         <div class="main-toolbar">
            <a-space>
               <a-button type="primary" @click="showAddNotice">
                  <template #icon>
                     <icon-plus />
                  </template>
                  新增</a-button>
               <a-button status="success" :disabled="selectedKeys.length !== 1" @click="handleUpdate">
                  <template #icon>
                     <icon-edit />
                  </template>
                  修改</a-button>
               <a-button status="danger" :disabled="selectedKeys.length === 0" @click="handleDelete">
                  <template #icon>
                     <icon-delete />
                  </template>
                  删除</a-button>
            </a-space>
         </div>
         <div class="main-content">
            <a-table :columns="columns" :data="noticeList" row-key="noticeId"
               :row-selection="{ type: 'checkbox', showCheckedAll: true }" @selection-change="handleSelectionChange">
               <template #noticeType="{ record }">
                  <dict-tag :options="sys_notice_type" :value="record.noticeType" />
               </template>
               <template #status="{ record }">
                  <dict-tag :options="sys_notice_status" :value="record.status" />
               </template>
            </a-table>
         </div>
      </div>
   </div>
   <NoticeForm ref="noticeFormRef"></NoticeForm>
</template>

<script setup name="Notice">
import { addNotice, delNotice, getNotice, listNotice, updateNotice } from "@/api/system/notice";
import NoticeForm from './noticeForm';
const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict("sys_notice_status", "sys_notice_type");

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      noticeTitle: undefined,
      createBy: undefined,
      status: undefined
   },
   rules: {
      noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
      noticeType: [{ required: true, message: "公告类型不能为空", trigger: "change" }]
   },
});

const { queryParams, form, rules } = toRefs(data);

const columns = ref([
   { key: 0, title: '序号', dataIndex: 'noticeId' },
   { key: 1, title: '公告标题', dataIndex: 'noticeTitle', align: 'center' },
   { key: 2, title: '公告类型', dataIndex: 'noticeType', slotName: 'noticeType' },
   { key: 3, title: '状态', dataIndex: 'status', slotName: 'status' },
   { key: 4, title: '创建者', dataIndex: 'createBy' },
   { key: 5, title: '创建时间', dataIndex: 'createTime' },
   { key: 6, title: '操作', dataIndex: 'operation' }
])
const selectedKeys = ref([])
/** 查询公告列表 */
function getList() {
   loading.value = true;
   listNotice(queryParams.value).then(({ data }) => {
      noticeList.value = data.rows;
      total.value = data.total;
      loading.value = false;
   });
}
/** 取消按钮 */
function cancel() {
   open.value = false;
   reset();
}
/** 表单重置 */
function reset() {
   form.value = {
      noticeId: undefined,
      noticeTitle: undefined,
      noticeType: undefined,
      noticeContent: undefined,
      status: "0"
   };
   proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   selectedKeys.value = selection
}
/** 新增按钮操作 */
function showAddNotice() {
   proxy.$refs.noticeFormRef.init('add')
}
/**修改按钮操作 */
function handleUpdate(row) {
   reset();
   const noticeId = row.noticeId || ids.value;
   getNotice(noticeId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改公告";
   });
}
/** 提交按钮 */
function submitForm() {
   proxy.$refs["noticeRef"].validate(valid => {
      if (valid) {
         if (form.value.noticeId != undefined) {
            updateNotice(form.value).then(response => {
               proxy.$modal.msgSuccess("修改成功");
               open.value = false;
               getList();
            });
         } else {
            addNotice(form.value).then(response => {
               proxy.$modal.msgSuccess("新增成功");
               open.value = false;
               getList();
            });
         }
      }
   });
}
/** 删除按钮操作 */
function handleDelete(row) {
   const noticeIds = row.noticeId || ids.value
   proxy.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function () {
      return delNotice(noticeIds);
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}

getList();
</script>
